/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import abstractmodel.IBook;
import abstractmodel.IInfo;
import abstractmodel.StatisticsType;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fightfxj
 */
public class User implements abstractmodel.IUser {

    private UserInfo userInfo;
    private ArrayList<IBook> books;

    public User() {
        userInfo = new UserInfo();
        books = new ArrayList<IBook>();
    }

    @Override
    public IInfo getUserInfo() {
        return userInfo;
    }

    @Override
    public boolean setUserInfo(IInfo info) {
        userInfo = (UserInfo) info;
        this.commit();
        return true;
    }

    @Override
    public boolean checkCode(String code) {
        String md5Password = (String) ModelFactory.data.loadLogInInfo().get(this.userInfo.getUserName());
        System.out.println("md5Password:"+md5Password);
        if (md5Password == null) {
            return false;
        } else {
            try {
                String md5code;
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(code.getBytes());
                MessageDigest change = (MessageDigest) md.clone();
                byte[] changeBytes = change.digest();
                md5code = new String(Arrays.toString(changeBytes));
                return md5code.equals(md5Password);
            } catch (CloneNotSupportedException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }


        }
    }

    @Override
    public boolean addNewBook(IBook book) {
        ((Book)book).setOwnerUser(this);
        books.add(book);
        this.commit();
        return true;
    }

    @Override
    public boolean deleteBook(IBook book) {
        if (books.remove(book)) {
                this.commit();
                return true;
        }
        else
                return false;      
    }

    @Override
    public ArrayList<IBook> getBooks() {
        return books;
    }

    @Override
    public StatisticsInfo getStatistics(StatisticsType type) {
        StatisticsInfo sinfo = new StatisticsInfo();
        for (IBook b : books) {
            sinfo.add(b.getBookInfo().getValue("账簿名称").getString(), new Double(b.getTotalLeftMoney()));
        }
        sinfo.setType(type);
        return sinfo;
    }

    @Override
    public boolean commit() {
        return ModelFactory.data.saveUser(this);
    }

    @Override
    public double getTotalLeftMoney() {
        double total = 0.0;
        for (IBook b : books) {
            total += b.getTotalLeftMoney();
        }
        return total;
    }
    
    /**
     * 打印用户的所有相关信息，供调试用
     */
    public void printAllUserInfo() {
        //titles = new String[]{"用户名", "密码", "客户姓名", "性别", "联系电话"}
         System.out.println("Titles : ");
        for (int i = 0;i < userInfo.getTitles().length; i ++)
            System.out.print(userInfo.getTitles()[i] +  " ");
        System.out.println();         
        System.out.println("用户名：" + userInfo.getValue("用户名").getString());
        System.out.println("密码：" + userInfo.getValue("密码").getString());
        System.out.println("客户姓名：" + userInfo.getValue("客户姓名").getString());
        System.out.println("性别：" + userInfo.getValue("性别").getString());
        System.out.println("联系电话：" + userInfo.getValue("联系电话").getString());
        System.out.println("ID：" + userInfo.getValue("id").getString());
    
    }

    @Override
    public ArrayList<IBook> getBooksLeftMoneyMoreThan(double d) {
        ArrayList<IBook> booksWant = new ArrayList<IBook>();
        for (IBook b : this.getBooks()) {
            if (b.getTotalLeftMoney() > d)
                booksWant.add(b);
        }
        return booksWant;
    }
}
